<?php


class EditAppxFeature{
	
	public $features;
	public $applications;
	
	
	public function __construct(){

		
		switch ($_SERVER['REQUEST_METHOD']) {
			case 'GET':
				
				switch( $_GET['action']){
					case 'checked':
						
						global $app;
						
						
						$result	=	$this->setAppxFeature( (object) $_GET ,$app->getUser_id() );
		
						
						if( $result==1){
							echo "<img src='images/icon/drop.png' />";
						}else{
							echo "&nbsp;";
						}
						
						exit("");
						header("Location: gridAppxFeature.php", TRUE, 301); 
						
						
					case 'del':
						
						
						break;

						
					default:
						
						global $app;

						$this->rowslist		=	$this->getRows( $app->getUser_id() );
						
						
						break;
				}
				
				
				break;
			case 'POST':
										
				
				switch( $_POST['action']){
					case 'edit':
						
						global $app;
						
						$_POST['user_id']=	$app->getUser_id();
						$result			=	$this->setFeature( (object) $_POST );						
					
						if(empty($_POST['feature_id'])){
							
						}else{
							$this->id	=	$_POST['feature_id'];
						}
						
						
						$this->nome		=	$_POST['nome'];						
						$this->detalhe	=	$_POST['detalhe'];	
						$this->user_id	=	$_POST['user_id'];	
						$this->ordem	=	$_POST['ordem'];	
		
						
						break;
						
					case 'del':
						
						global $app;
						
						$user_id		=	$app->getUser_id();
						$result			=	$this->removeFeature( $_POST['feature_id'], $user_id );
						
						header("Location: index.php", TRUE, 301); 
						
						break;
						
					default:
						
						break;
				}
				
				
				break;
			default:
				exit("Caia fora");
				break;
		}
		

		
	}

	
	function splitWhere( $where )
	{
		
		return ( count( $where ) ? 
								' WHERE ' . implode( ' AND ', $where ) : 
								'' );
	}
		
	
	public function getGrid( $rows ){
		
		$result = "";
		
		global $app;
		
		$this->features		=	$this->getAllFeature( $app->getUser_id() );
		$this->applications	=	$this->getAllApplication( $app->getUser_id() );
						
		$matriz_app=array();
		
		foreach( $this->applications as $key=>$item){
			
			//$matriz[$item->id]=$item->nome;			
			array_push( $matriz_app, array($item->id=>$item->nome));
		}

		$matriz_features=array();
		
		foreach( $this->features as $key=>$item){
			
			array_push( $matriz_features, array($item->id=>$item->nome));
			//$matriz[0][$item->id]=$item->nome;
		}
	
		
		
		
		
		$colors=array('#ffffff','#cccccc');
		$colorsLinhas=array('#ffffff','#FFFF00');
		$cor=0;
		$cor1=0;
		$cabecalho=TRUE;
		foreach( $this->features as $key=>$feature){

			
			if( $cabecalho==TRUE){
				
				$result .=	"<tr>\n";							
				$result .=	"<td style='width:300px;height:220px;float:left;'>&nbsp;</td>\n";
				
				
				foreach( $this->applications as $key=>$application){
			
					$result .=	"<td style='background-color:".$colors[$cor].";vertical-align:bottom;height:210px'><a title='".$application->nome."' target='_blank' href='editApp.php?action=edit&application_id="
								.$application->id."'><p class='css-vertical-text'>".$this->removeacentuacao($application->nome)."</p></a></td>\n";	
					
					if( $cor )
						$cor=0;
					else
						$cor=1;
					
					// $result .=	"<td>
// 					
					// <pre><code><span class='pln'>".$this->quebraLetras()."</span></code></pre>
// 					
					// <a title='".$feature->nome."' target='_blank' href='editFeature.php?action=edit&feature_id="
								// .$feature->id."'><img src='http://localhost:8888/bifm/survey/img.php?font=4&color="
								// .urlencode("#FFFFFF")."&text=".urlencode($feature->nome)."' ></a></td>\n";	
								
								
					//$result .=	"<td class='rotated_cell'><div class='rotate_text'>".$feature->nome."</div></td>\n";				
				
				}
				
				$cabecalho=FALSE;
				$result .="</tr>";
				
			}

			$cor=0;
			$result .=	"<tr>\n";							
			$result .=	"<td style='background-color:".$colorsLinhas[$cor1].";'><a target='_blank' href='editFeature.php?action=edit&feature_id=".$feature->id."'><p class='css-horizontal-text'>".$feature->nome."</p></a></td>\n";
			
			if( $cor1 )
				$cor1=0;
			else
				$cor1=1;
							
			foreach( $this->applications as $keyApplication=>$application){
				
				$contem=FALSE;
												
				foreach( $rows as $keyRow=>$item){
						
					if( $application->id == $item->application_id 
						&& $feature->id == $item->feature_id ){
						
						$result .=	"<td align='center' style='background-color:".$colors[$cor].";'>"
									."<a title='".$feature->nome." -> ".$application->nome."' style='width:100%;height:100%;float:left;' 
									onclick='javascript:changeFeatureApp(".$application->id.",".$feature->id.", \"id"
									.$application->id."".$feature->id."\")' href='javascript:void(0);' title='Sim' id='id".$application->id."".$feature->id."'><img src='images/icon/drop.png' /></a>"."</td>\n";
									
						$contem=TRUE;
						
						if( $cor )
							$cor=0;
						else
							$cor=1;
					
					}				
					
					
							
				}		
				
				if( $contem==FALSE )	{
					$result .=	"<td align='center' style='background-color:".$colors[$cor].";'>"
									."<a title='".$feature->nome." -> ".$application->nome."' style='width:100%;height:100%;float:left;' 
									onclick='javascript:changeFeatureApp(".$application->id.",".$feature->id.", \"id"
									.$application->id."".$feature->id."\")' href='javascript:void(0);' title='Não' id='id".$application->id."".$feature->id."'>&nbsp;</a>"."</td>\n";
						
					if( $cor )
							$cor=0;
						else
							$cor=1;
					
				}
				
					
			}
			
			$result .="</tr>\n";
	
		}
		
		
		
		
		
		
		
		
		
		// $cabecalho=TRUE;
		// foreach( $this->applications as $key=>$application){
// 
// 			
			// if( $cabecalho==TRUE){
// 				
				// $result .=	"<tr>\n";							
				// $result .=	"<td>&nbsp;</td>\n";
// 				
				// foreach( $this->features as $key=>$feature){
// 			
					// $result .=	"<td style=' vertical-align:bottom;height:250px'><a title='".$feature->nome."' target='_blank' href='editFeature.php?action=edit&feature_id="
								// .$feature->id."'><p class='css-vertical-text'>".$this->removeacentuacao($feature->nome)."</p></a></td>\n";	
// 					
// 					
					// // $result .=	"<td>
// // 					
					// // <pre><code><span class='pln'>".$this->quebraLetras()."</span></code></pre>
// // 					
					// // <a title='".$feature->nome."' target='_blank' href='editFeature.php?action=edit&feature_id="
								// // .$feature->id."'><img src='http://localhost:8888/bifm/survey/img.php?font=4&color="
								// // .urlencode("#FFFFFF")."&text=".urlencode($feature->nome)."' ></a></td>\n";	
// 								
// 								
					// //$result .=	"<td class='rotated_cell'><div class='rotate_text'>".$feature->nome."</div></td>\n";				
// 				
				// }
// 				
				// $cabecalho=FALSE;
				// $result .="</tr>";
// 				
			// }
// 
// 		
			// $result .=	"<tr>\n";							
			// $result .=	"<td><a target='_blank' href='editApp.php?action=edit&application_id=".$application->id."'>".$application->nome."</a></td>\n";
// 				
			// foreach( $this->features as $keyFeature=>$feature){
// 				
				// $contem=FALSE;
// 				
				// foreach( $rows as $keyRow=>$item){
// 						
					// if( $application->id == $item->application_id 
						// && $feature->id == $item->feature_id ){
// 						
						// $result .=	"<td align='center'>"
									// ."<a style='width:100%;height:100%;float:left;' 
									// onclick='javascript:changeFeatureApp(".$application->id.",".$feature->id.", \"id"
									// .$application->id."".$feature->id."\")' href='#' title='Sim' id='id".$application->id."".$feature->id."'><img src='images/icon/drop.png' /></a>"."</td>\n";
// 									
						// $contem=TRUE;
					// }				
// 							
				// }		
// 				
				// if( $contem==FALSE )	
					// $result .=	"<td align='center'>"
									// ."<a style='width:100%;height:100%;float:left;' 
									// onclick='javascript:changeFeatureApp(".$application->id.",".$feature->id.", \"id"
									// .$application->id."".$feature->id."\")' href='#' title='Não' id='id".$application->id."".$feature->id."'>&nbsp;</a>"."</td>\n";
// 			
			// }
// 			
			// $result .="</tr>\n";
// 	
		// }
// 		
		
		
		
		
		
		
		
		
		
		
			// foreach( $matriz[$key] as $key2=>$item2 ){
// 				
				// $matriz[$key][$key2]=$item2;
// 				
			// }		
		
// 		
		// foreach( $matriz as $key=>$item){
// 			
// 			
// 			
			// if( $key !=0 ){
// 				
				// $result .="<tr>";
// 				
				// $result .=	"<td>".$matriz[$key]."</td>";
// 			
// 			
				// foreach( $matriz[0] as $key2=>$item2 ){
// 					
// 					
// 					
// 					
					// if( isset( $matriz[$key][$key2] ) ){
						// //var_dump( $matriz[$key] );
						// $result .=	"<td></td>";
					// }else{
						// $result .=	"<td>".$matriz[$key][$key2]."</td>";
					// }
// 
// 					
				// }
// 				
				// $result .="</tr>";
// 					
			// }
// 				
// 			
// 
// 
			// //$result .=	"<td>".$matriz[$item->application_id][$item->feature_id]."</td>";
			// //$result .=	"<td>".$item->detalhe."</td>";
			// //$result .=	"<td>".$item->ordem."</td>";
// 			
// 			
		// }
		
		//var_dump( $matriz );
		//exit();
		
		
		return "<table class='gridTable' cellspaccing=0 cellpadding=0>".$result."</table>";
	}	
	
	
	
	function removeacentuacao($text){
		
		$acentos = array(
	        'À','Á','Ã','Â', 'à','á','ã','â',
	        'Ê', 'É',
	        'Í', 'í', 
	        'Ó','Õ','Ô', 'ó', 'õ', 'ô',
	        'Ú','Ü',
	        'Ç', 'ç',
	        'é','ê', 
	        'ú','ü',
	        );
	    $remove_acentos = array(
	        'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
	        'e', 'e',
	        'i', 'i',
	        'o', 'o','o', 'o', 'o','o',
	        'u', 'u',
	        'c', 'c',
	        'e', 'e',
	        'u', 'u',
	        );
	    return str_replace($acentos, $remove_acentos, urldecode($text));
	
		
	}	
	
	private function quebraLetras( $text ){
		
		$result="";
		
		for($i=1; $i<=strlen($text); $i++)
		{
			$result .= substr($text, $i-1, 1)."\n";
		}

		return $result;
	}
			
	function getRows( $user_id=0, $limit_start=0, $limit_end=10 )
	{

		$result	=	"";
		$config	=	new Configuration();	
		
		$db 	=	new MySQL( $config->db_server, 
							$config->db_username, 
							$config->db_password, 
							$config->db_name );

		$where	=	array();

		$where[]=	'user_id = \''.$user_id.'\'';
		
		$where	=	$this->splitWhere( $where );
				
																
		$query 	=	'SELECT '
					. ' '				
					. ' * '
					. ' '
					. ' FROM '.$config->db_table_semantic_web_application_feature.' '
					. $where;

		$rows	=	$db->getQuery($query);
			
		
		if(count( $rows )>0 )
			$result=	$rows;

		
		return $result;
	}
	
	
			
	function getAllFeature( $user_id=0 )
	{

		$result	=	"";
		$config	=	new Configuration();	
		
		$db 	=	new MySQL( $config->db_server, 
							$config->db_username, 
							$config->db_password, 
							$config->db_name );

		$where	=	array();

		$where[]=	'user_id = \''.$user_id.'\'';
		
		$where	=	$this->splitWhere( $where );
				
																
		$query 	=	'SELECT '
					. ' '				
					. ' * '
					. ' '
					. ' FROM '.$config->db_table_semantic_web_feature.' '
					. $where;

		$rows	=	$db->getQuery($query);
		
		if(count( $rows )>0 )
			$result=	$rows;
		
		return $result;
	}
	
	
	function getAllApplication( $user_id=0 )
	{

		$result	=	"";
		$config	=	new Configuration();	
		
		$db 	=	new MySQL( $config->db_server, 
							$config->db_username, 
							$config->db_password, 
							$config->db_name );

		$where	=	array();

		$where[]=	'user_id = \''.$user_id.'\'';
		
		$where	=	$this->splitWhere( $where );
				
																
		$query 	=	'SELECT '
					. ' '				
					. ' * '
					. ' '
					. ' FROM '.$config->db_table_semantic_web_application.' '
					. $where;

		$rows	=	$db->getQuery($query);
		
		if(count( $rows )>0 )
			$result=	$rows;
		
		return $result;
	}
	
	
	
	
	function getAppxFeature( $user_id=0, $application_id=0, $feature_id=0 )
	{

		$result	=	"";
		$config	=	new Configuration();	
		
		$db 	=	new MySQL( $config->db_server, 
							$config->db_username, 
							$config->db_password, 
							$config->db_name );

		$where	=	array();

		$where[]=	'feature_id = '.$feature_id.'';
		$where[]=	'application_id = '.$application_id.'';
		$where[]=	'user_id = '.$user_id.'';
		
		$where	=	$this->splitWhere( $where );
				
																
		$query 	=	'SELECT '
					. ' '				
					. ' * '
					. ' '
					. ' FROM '.$config->db_table_semantic_web_application_feature.' '
					. $where;

		$rows	=	$db->getQuery($query);
		
		if(count( $rows )>0 )
			$result=	$rows[0];
		
		return $result;
	}
	
	
	
	
	function removeAppxFeature( $user_id, $application_id, $feature_id )
	{

		$result	=	"";
		$config	=	new Configuration();	
		
		$db 	=	new MySQL( $config->db_server, 
							$config->db_username, 
							$config->db_password, 
							$config->db_name );
												
		
		$where	=	array();
			
		$where[]=	'application_id = '.$application_id.'';
		$where[]=	'feature_id = '.$feature_id.'';		
		$where[]=	'user_id = '.$user_id.'';
		
		$where	=	$this->splitWhere( $where );
				
		//$result	=	$this->getAppxFeature( $user_id, $application_id, $feature_id );
		
		
		//if( empty($result) ){
			//insert
			
			$query 	=	'DELETE FROM '
						. '  '.$config->db_table_semantic_web_application_feature.' '
						.$where;
						
		//}


		$rows	=	$db->setQuery($query);
		
		if(count( $rows )>0 )
			$result=	$rows[0];
		
		return $result;
	}


	function setAppxFeature( $args, $user_id )
	{

		$result	=	"";
		$config	=	new Configuration();	
		
		$db 	=	new MySQL( $config->db_server, 
							$config->db_username, 
							$config->db_password, 
							$config->db_name );
												
		
		$where	=	array();
			
		$where[]=	'feature_id = '.$args->feature_id.'';
		$where[]=	'application_id = '.$args->application_id.'';
		$where[]=	'user_id = '.$user_id.'';
		
		$where	=	$this->splitWhere( $where );
		
		

		$exists	=	$this->getAppxFeature( $user_id, $args->application_id, $args->feature_id );
		
		if( empty($exists ) ){
		//insert
		
			$query 	=	'INSERT INTO '
					. '  '.$config->db_table_semantic_web_application_feature.' '
					.' (application_id, feature_id, user_id, data_cadastro) '
					.' values '
					.' ('.addslashes($args->application_id).','
					.' '.addslashes($args->feature_id).','
					.' '.addslashes($user_id).','
					.' now() )';
					
			$result=1;		
		}else{
			
			$result=	$this->removeAppxFeature( $user_id, $args->application_id, $args->feature_id );
			$result=0;
		}
			
			// else{
				// //update
// 																		
				// $query 	=	'UPDATE '
							// . '  '.$config->db_table_semantic_web_application_feature.' '
							// .' SET '
							// .' application_id="'.addslashes($args->application_id).'",'
							// .' feature_id="'.addslashes($args->feature_id).'",'
							// .' user_id='.addslashes($user_id).', '
							// .' data_atualizacao=now() '						
							// . $where;
// 				
			// }
				
			

		
		
		//exit($query);
		$rows	=	$db->setQuery($query);
		
		
		
		return $result;
	}
	
	
}

?>